package io.dbw.common.utils;

import io.dbw.modules.business.entity.SysMenuEntity;
import io.dbw.modules.business.vo.ChildMenuVO;
import io.dbw.modules.business.vo.ChildMetaVO;
import io.dbw.modules.business.vo.MenuVO;
import io.dbw.modules.business.vo.MetaVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lxc
 * @description:
 * @date 2022/2/10 16:40
 */
public class ListUtils {

    public static List<SysMenuEntity> removeDuplicateDept(List<SysMenuEntity> deptS) {
        if (CollectionUtils.isEmpty(deptS)) {
            return new ArrayList<>();
        }
        Set<SysMenuEntity> set = new TreeSet<SysMenuEntity>((m1, m2) -> m1.getMenuId().compareTo(m2.getMenuId()));
        set.addAll(deptS);
        return new ArrayList<>(set);
    }

    public static List<MenuVO> getMenuList(List<SysMenuEntity> mList, int type) {
        List<MenuVO> reList = new ArrayList<MenuVO>();
        if (!CollectionUtils.isEmpty(mList)) {
            List<MenuVO> thList = new ArrayList<MenuVO>();
            mList.stream().forEach(sm -> {
                MenuVO smv = new MenuVO();
                smv.setMenuId(sm.getMenuId());
                String path = sm.getUrl();
                if (type == 1) {
                    smv.setName(sm.getTitle());
                } else {
                    smv.setName(sm.getName());
                }
                smv.setRedirect(sm.getIsFrame());
                smv.setParentId(sm.getParentId());
                smv.setMenuType(sm.getType());
                MetaVO meta = new MetaVO();
                meta.setIcon(sm.getIcon());
                meta.setTitle(sm.getTitle());
                smv.setMeta(meta);
                smv.setPath(path);
                smv.setOrderNum(sm.getOrderNum());
                smv.setHidden("1".equals(sm.getVisible()) ? false : true);
                smv.setComponent(sm.getComponent());
                smv.setPerms(sm.getPerms());
                smv.setAlwaysShow(true);
                if ("Redirect".equals(sm.getName())) {
                    smv.setAlwaysShow(false);
                    if (type == 1) {
                        smv.setName(sm.getTitle());
                    } else {
                        smv.setName(sm.getName());
                    }
                    smv.setMeta(new MetaVO());
                }
                thList.add(smv);
            });
            reList = getMenuTree(thList);
        }
        return reList;
    }

    public static List<MenuVO> getMenuTree(List<MenuVO> list) {
        //获取父节点
        List<MenuVO> collect = list.stream().filter(m -> m.getParentId() == 0)
                .map((m) -> {
                    m.setChildren(getChildrens(m, list));
                    return m;
                }).sorted(Comparator.comparing(MenuVO::getOrderNum)).collect(Collectors.toList());
        return collect;
    }

    /**
     * 递归查询子节点
     *
     * @param root 根节点
     * @param all  所有节点
     * @return 根节点信息
     */
    private static List<ChildMenuVO> getChildrens(MenuVO root, List<MenuVO> all) {
        List<ChildMenuVO> children = all.stream().filter(m -> {
            return Objects.equals(m.getParentId(), root.getMenuId());
        }).map((m) -> {
                    m.setChildren(getChildrens(m, all));
                    ChildMenuVO cm = BeanUtil.copyProperties(m, ChildMenuVO.class);
                    ChildMetaVO cv = new ChildMetaVO();
                    cv.setAffix(false);
                    if (StringUtils.isNotBlank(m.getRedirect()) && "dashboard".equals(m.getRedirect())) {
                        cv.setAffix(true);
                    }
                    cv.setIcon(null != m.getMeta() ? m.getMeta().getIcon() : "");
                    cv.setTitle(null != m.getMeta() ? m.getMeta().getTitle() : "");
                    cm.setMeta(cv);
                    return cm;
                }
        ).sorted(Comparator.comparing(ChildMenuVO::getOrderNum)).collect(Collectors.toList());
        return children;
    }

}
